import appuifw, e32, graphics, globalui, time
__author__ = "John Gray-Spence"

class Event:
	def __init__(self)
		self.dayList = list()	 #this is the list of days
		self.eventList = list()	#This list of events to display

	def popDays():
		eventList.append({"subject": "cp3120", "type": "lecture", "from": "1300", "until": "1500", "location": "E1.103", "weekly": True})
		
		self.dayList.append({"day": "Monday", "event": list()})
		self.dayList.append({"day": "Tuesday", "event": list()})
		self.dayList.append({"day": "Wednesday", "event": list()})
		self.dayList.append({"day": "Thursday", "event": list()})
		self.dayList.append({"day": "Friday", "event": list()})
		self.dayList.append({"day": "Saturday", "event": list()})
		self.dayList.append({"day": "Sunday", "event": list()})

	def addEvent():
		loopOp = True
		
		while loopOp == True:
			#userDay = appuifw.query(u"Day", "number") -1
			userDay = globalui.global_popup_menu([u"Monday",u"Tuesday",u"Wednesday",u"Thursday",u"Friday",u"Saturday",u"Sunday",],u"Day")
			#textArea.add(unicode(userDay))
			#textArea.add(unicode(dayList)) #debugging
			textArea.add(unicode(dayList[userDay]["day"] + " chosen"))
			textArea.add(u"\n")
			
			#the following will need error checking in the final thing for correct data input, but this is not the final thing
			eventSubject = appuifw.query(u"Event subject", "text")	#in GUI this will be a drop down box of available subjects
			#eventType = appuifw.query(u"Event type", "text")		#this will be selected as well in the GUI
			eventType = globalui.global_popup_menu([u"Lecture",u"Practical",u"Tutorial"],u"Type")	#User does not have to type it
			#if eventType = Lecture
			#	lecturer	= raw_input("Lecturer: ")
			eventFrom = appuifw.query(u"Start time: ", "time")
			eventUntil = appuifw.query(u"End time: ", "time")
			eventLocation = appuifw.query(u"Location: ", "text")
			eventWeekly = appuifw.query(u"Weekly: ", "query")

			eventEntry = {"subject": eventSubject, "type": eventType, "from": eventFrom, "until": eventUntil, "location": eventLocation, "weekly": eventWeekly}
			eventList.append(eventEntry)
			dayList[userDay]["event"].append(eventEntry)

			appuifw.note(u"Event added", "conf")
			loopOp = appuifw.query(u"Continue: ", "query")

	def deleteEvent():
		targetDay = globalui.global_popup_menu([u"Monday",u"Tuesday",u"Wednesday",u"Thursday",u"Friday",u"Saturday",u"Sunday",],u"Day")
		targetEvent = dayList[targetDay]
	#	for day in dayList:
	#		if day["day"] == targetDay:
	#		 if len(day["event"]) == 0: #check if there is an event to delete
	#				textArea.add(u"No event to delete")
	#		 else:
		textArea.add(u"Events:")
		textArea.add(u"\n")
		eventNo = 1
		for event in targetEvent["event"]:
			textArea.add(unicode(eventNo))
			textArea.add(u". ")
			textArea.add(unicode(event["subject"]))
			textArea.add(u", ")
			textArea.add(unicode(event["type"]))
			textArea.add(u", ")
			textArea.add(unicode(time.ctime(event["from"])))
			textArea.add(u"\n")
			eventNo += 1
					
		deleteOp = appuifw.query(u"Which do you want to delete: ", "number") - 1
		del (dayList[targetDay]["event"][deleteOp])
		textArea.add(u"\n")
		
	def printDayList(daysOnly = True):
		for day in dayList:
			if daysOnly:
				textArea.add(u"Working day is: %s" % day["day"])
				textArea.add(u"\n")
			else:
				textArea.add(unicode(day))
				textArea.add(u"\n")

	def printDaysEvents():
		workingDay = globalui.global_popup_menu([u"Monday",u"Tuesday",u"Wednesday",u"Thursday",u"Friday",u"Saturday",u"Sunday",],u"Day")
		workingEvent = dayList[workingDay]
		textArea.add(u"Data for working event: %s" % str(workingEvent["day"]))
		textArea.add(u"\n")
		try:
			for event in workingEvent["event"]:
				textArea.add(unicode(event["subject"]))
				textArea.add(u", ")
				textArea.add(unicode(event["type"]))
				textArea.add(u", ")
				textArea.add(unicode(time.ctime(event["from"])))
				textArea.add(u"\n")
		except KeyError, e:
			textArea.add(u"Selected day (%s) doesn't have any events. Code: %s" % (chosenDay["day"], e))
			textArea.add(u"\n")
			
	def getDaysEvents(self, day):
		eventStr = ""
		workingEvent = self.dayList[day]
		# Should produce "subject:eventType:fromhour:fromminute:untilhour:untilminute="
		for event in workingEvent["event"]:
			eventStr+= event["subject"] + ":" + event["type"] + ":" time.localtime(event["from"])[tm_hour] + ":" + time.localtime(event["from"])[tm_minute]) + ":" + time.localtime(event["until"])[tm_hour] + ":" + time.localtime(event["until"])[tm_minute]) + "="
		return eventStr

	popDays() #creates the database